import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {

    static class TreeNode {
        public char val;
        //存储左孩子节点的引用
        public TreeNode left;
        //存储右孩子节点的引用
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }


    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;

        return A;
    }

    //前序遍历
    public void Qbianli(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val+" ");
        Qbianli(root.left);
        Qbianli(root.right);
    }

    //中序遍历
    public void Zbianli(TreeNode root) {
        if(root == null) {
            return;
        }
        Zbianli(root.left);
        System.out.print(root.val+" ");
        Zbianli(root.right);
    }
    //后序遍历
    public void Hbianli(TreeNode root) {
        if(root == null) {
            return;
        }
        Hbianli(root.left);
        Hbianli(root.right);
        System.out.print(root.val+" ");
    }

    //计算节点个数方法一
    public int count = 0;
    public int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        count++;
        size1(root.left);
        size1(root.right);
        return count;
    }

    //计算节点个数方法二
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size2(root.left) + size2(root.right) + 1;
    }

    //计算叶子个数方法一
    public int ye = 0;
    public int sizeye1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            ye++;
        }
        sizeye1(root.left);
        sizeye1(root.right);
        return ye;
    }

    //计算叶子个数方法二
    public int sizeye2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return sizeye2(root.left) + sizeye2(root.right);

    }

    //计算树第k层节点个数
    public int tree(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if(k==1) {
            return 1;
        }
        return tree(root.left,k-1) + tree(root.right,k-1);
    }

    //计算树的钱高度
    public int Hight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int left = Hight(root.left);
        int right = Hight(root.right);
        return left > right ?  left + 1 : right + 1 ;
    }

//    public TreeNode find(TreeNode root,int val) {
//        if(root == null) {
//            return null;
//        }
//        if(root.val == val) {
//            return root;
//        }
//        return find(root.left);
//        return find(root.right);
//
//    }

//    if(root==null) {
//        return null;
//    }
//    TreeNode k = null;
//    k = root.left;
//    root.left

    public TreeNode value(TreeNode root,char val) {
        if(root==null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }

        TreeNode leftcur = value(root.left,val);
        if(leftcur != null) {
            return leftcur;
        }

        TreeNode rightcur = value(root.right,val);
        if(rightcur != null) {
            return rightcur;
        }
        return null;
    }

    public void prin(TreeNode root) {
        if(root == null) {
            return;
        }
        Queue<TreeNode> list = new LinkedList<>();

        list.offer(root);
        while(!list.isEmpty()) {

            TreeNode cur = list.poll();
            System.out.print(cur.val+" ");

            if(cur.left != null) {
                list.offer(cur.left);
            }
            if(cur.right != null) {
                list.offer(cur.right);
            }
        }
        System.out.println();
    }

//    class Solution {
//        public boolean isSameTree(TreeNode p, TreeNode q) {
//            if((p != null && q==null) ||( p == null && q != null)) {
//                return false;
//            }
//            if(p == null && q == null) {
//                return true;
//            }
//            if(p.val != q.val) {
//                return false;
//            }
//            return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
//        }
//    }


//    class Solution {
//        public TreeNode invertTree(TreeNode root) {
//            if(root == null) {
//                return null;
//            }
//            TreeNode cur = root.left;
//            root.left = root.right;
//            root.right = cur;
//
//            invertTree(root.left);
//            invertTree(root.right);
//
//            return root;
//        }
//    }
//class Solution {
//    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
//        if(root== null) {
//            return false;
//        }
//        if(isSameTree(root,subRoot)) {
//            return true;
//        }
//        if(isSubtree(root.left,subRoot)) {
//            return true;
//        }
//        if(isSubtree(root.right,subRoot)) {
//            return true;
//        }
//        return false;
//    }
//    public boolean isSameTree(TreeNode p, TreeNode q) {
//        if(p != null && q==null || p == null && q != null) {
//            return false;
//        }
//        if(p == null && q == null) {
//            return true;
//        }
//        if(p.val != q.val) {
//            return false;
//        }
//        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
//    }
//}
}